package com.zyndev.tool.validation.core;

import com.zyndev.tool.validation.ValidResult;
import com.zyndev.tool.validation.constraints.NotNull;

import java.lang.reflect.Field;

/**
 * desc :
 * author: 张瑀楠
 * email : zyndev@gmail.com
 * date  : 2017/12/1 下午10:02
 * todo  :
 */
public class NotNullValid<T> implements ValidRole<T> {

    @Override
    public ValidResult execute(T t, Field field) {
        NotNull notNull = field.getAnnotation(NotNull.class);
        if (null != notNull) {
            try {
                field.setAccessible(true);
                if (null == field.get(t)) {
                    return new ValidResult(false, notNull.message());
                }
            } catch (Exception e) {
                System.out.println(e);
                return new ValidResult(false, e.getMessage());
            }
        }
        return new ValidResult(true, "");
    }
}
